package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * 踢出群聊
 */
@MessageTag(value = IMMessageType.GROUP_KICK, flag = MessageTag.ISPERSISTED | MessageTag.ISCOUNTED)
public class RXGroupKickContent extends MessageContent {

    private String content;
    private String quiteGroupUserId;
    private String quiteGroupUserName;

    @Override
    public byte[] encode() {
        JSONObject object = new JSONObject();
        object.put("content", content);
        object.put("quiteGroupUserId", quiteGroupUserId);
        object.put("quiteGroupUserName", quiteGroupUserName);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RXGroupKickContent() {

    }

    public RXGroupKickContent(byte[] data) {
        super(data);
        String jsonStr = null;
        try {
            jsonStr = new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);
            setContent(object.getString("content"));
            setQuiteGroupUserId(object.getString("quiteGroupUserId"));
            setQuiteGroupUserName(object.getString("quiteGroupUserName"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, content);
        ParcelUtils.writeToParcel(dest, quiteGroupUserId);
        ParcelUtils.writeToParcel(dest, quiteGroupUserName);
    }

    public static final Creator<RXGroupKickContent> CREATOR = new Creator<RXGroupKickContent>() {

        @Override
        public RXGroupKickContent createFromParcel(Parcel source) {
            return new RXGroupKickContent(source);
        }

        @Override
        public RXGroupKickContent[] newArray(int size) {
            return new RXGroupKickContent[size];
        }
    };


    public RXGroupKickContent(Parcel parcel) {
        content = ParcelUtils.readFromParcel(parcel);
        quiteGroupUserId = ParcelUtils.readFromParcel(parcel);
        quiteGroupUserName = ParcelUtils.readFromParcel(parcel);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getQuiteGroupUserId() {
        return quiteGroupUserId;
    }

    public void setQuiteGroupUserId(String quiteGroupUserId) {
        this.quiteGroupUserId = quiteGroupUserId;
    }

    public String getQuiteGroupUserName() {
        return quiteGroupUserName;
    }

    public void setQuiteGroupUserName(String quiteGroupUserName) {
        this.quiteGroupUserName = quiteGroupUserName;
    }
}

